# -*- coding:utf-8 -*-

# @Time : 2023/6/2 10:00
# @Author : 快乐的小猴子
# @Version : 
# @Function :

import time
import os
from CONFIG import setting
from lxml import etree
from BaseClass import BaseClass


class ShelfBooks(BaseClass):

    def __init__(self, login_url, username, password):
        """
        初始化变量
        :param login_url: 登录地址 url
        :param username: 用户名
        :param password: 密码
        """
        self.username = username
        self.password = password
        self.login_url = login_url

    def login(self, login_url, login_data):
        """
        登录平台
        :param login_url: 登录url
        :param login_data: 登录携带的数据
        :return: resp 响应回的数据
        """
        ses_resp = setting.session.post(login_url, data=login_data, headers=setting.HEADERS)
        return ses_resp.status_code

    def get_total_cnt(self):
        """
        获取总页数
        :return: page_cnt
        """
        resp = self.send(setting.URL[0], 1).json()
        totalNum = resp['totalNum']
        if totalNum % 20 == 0:
            page_cnt = int(totalNum / 20)
        else:
            page_cnt = int(totalNum / 20) + 1
        return page_cnt

    def handle_all_shelf_data(self, page_cnt, resp):
        """
        处理`我的书架`中所有的小说信息
        :param page_cnt:
        :param resp:
        :return:
        """
        num = 0
        all_books_data = []
        # 循环所有的页数
        for i in range(1, page_cnt+1):
        # for i in range(1, 2):
            one_page_data = resp['data']
            for j in range(0, len(one_page_data)):
                one_book_data = {}
                bookId = one_page_data[j]['bookId'] # ID
                bookName = one_page_data[j]['bookName'] # 书名
                authorPenName = one_page_data[j]['authorPenName'] # 作者笔名
                bookCategory = one_page_data[j]['bookCategory']['name'] # 分类
                bookStatus = one_page_data[j]['bookStatus']['name'] # 小说目前状态
                lastUpdateChapter = one_page_data[j]['lastUpdateChapter']['name'] # 最新章节名称
                isVIP = one_page_data[j]['lastUpdateChapter']['isVIP']['name'] # 最新章节是否需要VIP
                updateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(str(one_page_data[j]['lastUpdateChapter']['updateTime'])[:-3]))) # 最新章节更新时间
                introduction = one_page_data[j]['introduction'] # 介绍
                one_book_data['bookId'] = bookId
                one_book_data['bookName'] = bookName
                one_book_data['authorPenName'] = authorPenName
                one_book_data['bookCategory'] = bookCategory
                one_book_data['bookStatus'] = bookStatus
                one_book_data['lastUpdateChapter'] = lastUpdateChapter
                one_book_data['isVIP'] = isVIP
                one_book_data['updateTime'] = updateTime
                one_book_data['introduction'] = introduction
                all_books_data.append(one_book_data)
                num += 1
                print(num, bookId, bookName, authorPenName, bookCategory, bookStatus, lastUpdateChapter, isVIP,
                      updateTime, introduction)
        return num, all_books_data

    def verify_dir(self, dir):
        """
        校验存储文件路径是否存在，若不存在则创建
        :param dir:
        :return:
        """
        path = os.getcwd() + '\\files\\{}\\'.format(dir)
        if not os.path.exists(path):
            print('%s 路径不存在！！！' % path)
            os.makedirs(path)
            print('%s 路径创建完成！！！' % path)
        else:
            print('%s 路径已存在~~~' % path)
        return path

    def save_illu_file(self, num, all_books_data):
        """
        所有小说详情信息
        :param num:
        :param all_books_data:
        :return:
        """
        path = self.verify_dir(setting.dirs[0])
        book_name = path + 'Readme.csv'
        with open(book_name, 'w', encoding='utf-8') as fp:
            print('ID|书名|作者笔名|分类|小说目前状态|更新章节名称|最新章节是否需要VIP|最新章节更新时间\n')
            fp.write('序号|ID|书名|作者笔名|分类|小说目前状态|更新章节名称|最新章节是否需要VIP|最新章节更新时间\n')
            for i in range(0, len(all_books_data)):
                print('第{}条数据{}正在写入文件...'.format(i+1, all_books_data[i]['bookName']))
                fp.write(str(i+1) + '|' + str(all_books_data[i]['bookId']) + '|' + all_books_data[i]['bookName'] + '|' +
                         all_books_data[i]['authorPenName'] + '|' + all_books_data[i]['bookCategory'] + '|' +
                         all_books_data[i]['bookStatus'] + '|' + all_books_data[i]['lastUpdateChapter'] + '|' +
                         all_books_data[i]['isVIP'] + '|' + all_books_data[i]['updateTime'] + '|' +
                         all_books_data[i]['introduction'].strip().replace(' ', '').replace('\n', '') + '\n')
                print('第 %s 条：' % str(i+1), all_books_data[i]['bookName'] + '\t爬取成功~~~')
            print('本次共爬取我的书架{}条小说信息，信息记录在{} 路径下的《Readme.csv》文件中'.format(num, path))

    def get_detail_url(self, bookId):
        url_new = 'https://www.17k.com/list/%s.html' % str(bookId)
        return url_new

    def get_one_book_data(self, resp):
        et = etree.HTML(resp)
        chap_names = et.xpath('//div[@class="Main List"]/dl/dd/a/span/text()')
        chap_urls = et.xpath('//div[@class="Main List"]/dl/dd/a/@href')
        book_chaps_dic = {}
        # 循环每一个章节
        # for i in range(0, len(chap_names)):
        for i in range(0, 10):
            book_chaps_dic[chap_names[i].strip()] = chap_urls[i]
        return book_chaps_dic

    def down_book_datas(self, totao_cnt, all_books_data):
        path = self.verify_dir(setting.dirs[0])
        num=0
        # 循环每一本书
        for i in range(0, len(all_books_data)):
        # for i in range(0, 6):
            bookName = all_books_data[i]['bookName']
            with open(path + bookName + '.txt', 'w', encoding='utf-8') as fs:
                bookId = all_books_data[i]['bookId']
                url_new = self.get_detail_url(bookId)
                resp = self.send(url_new).content.decode()
                book_chaps_dic = self.get_one_book_data(resp)
                for k, v in book_chaps_dic.items():
                    chap_name = k
                    down_url = 'https://www.17k.com' + v
                    resp_cont = self.send(down_url)
                    resp_cont.encoding = 'utf-8'
                    tree = etree.HTML(resp_cont.text)
                    chap_conts = tree.xpath('//div[@class="readAreaBox content"]/div[@class="p"]/p/text()')
                    fs.write('{}\n{}'.format(chap_name, '\n'.join(chap_conts)))
                    print('{} 正在写入文件...'.format(k))
            print('《{}》爬取成功，文件保存在{} 路径下~~~'.format(bookName, path))
            num+=1
        print('`我的书架`共有{}本小说,本次共下载{}本小说，已保存在{} 路径~~~'.format(totao_cnt, num, path))


    def shelf_books_main(self):
        status_code = self.login(setting.LOGIN_URL, setting.LOGIN_DATA)
        if status_code != 200:
            print('login error !!!')
        else:
            resp = self.send(setting.URL[0], 1).json()
            page_cnt = self.get_total_cnt()
            total_cnt, all_books_data = self.handle_all_shelf_data(page_cnt, resp)
            self.save_illu_file(total_cnt, all_books_data)
            self.down_book_datas(total_cnt, all_books_data)




